(function () {
    $.AddForm = {
        Input:function (data) {

            //console.log(data);return false;
            var _html='';
            if(data.data){
               _html+=  _inputArea(data.data);
                $('.'+data.areaClass).prepend(_html);
            }else{
                $('.'+data.areaClass).prepend(InputHtml(0));
            }
            _addInput(data);
            _textTypeChange();//下拉框改变函数
            _addOption();
            _removeOption(data);
            _delInput(data);
        }
    };
    var InputHtml = function (num,id,name,fieldtype,required,isrepeat,option,_addOptionBtn) {
        num=num||'';id=id||'';name=name||'';fieldtype=fieldtype||'';required=required||'';isrepeat=isrepeat||'';option=option||'';
        _addOptionBtn = _addOptionBtn||'';
        var _inputHtml = '';
        _inputHtml += '<div class="textbox">' +
            '<div class="ipt_box2">' +
            '<div class="fromGroup show_box " style="margin-right: 12px;">' +
            '<input class="ipt_text" type="text" name="name" value="'+name+'" placeholder="请输入标题" style="width:430px; height:22px;border-color:#e1e1e1 " /> ' +
            '<div class="caseArea">'+option+'</div>'+_addOptionBtn+'</div>' +
            '<div class="fromGroup">' +
            '<select class=" slc_sort inputType" name="fieldtype"   style="width: 170px;height: 48px;border-color: #e1e1e1;padding-left: 10px;">' +
            '<option value="TextField" '+_selectIs(fieldtype,"TextField")+'>单行文本</option>' +
            '<option value="TextArea" '+_selectIs(fieldtype,"TextArea")+'>多行文本</option>' +
            '<option value="Radio" '+_selectIs(fieldtype,"Radio")+'>单项选择</option>' +
            '<option value="CheckBox" '+_selectIs(fieldtype,"CheckBox")+'>多项选择</option>' +
            '<option value="UploadPic" '+_selectIs(fieldtype,"UploadPic")+'>图片上传</option>' +
            '<option value="UploadFile" '+_selectIs(fieldtype,"UploadFile")+'>文件上传</option>' +
            '</select>' +
            '</div>' +
            '</div>' +
            '<div class="fromGroup remarkBtn rb_box1">' +
            '<input type="checkbox" class="checkbox" name="required" value="1" id="required_checkbox_'+num+'" '+_checkedIs(required,true)+'/>' +
            '<label for="required_checkbox_'+num+'">&nbsp;必填</label>&nbsp;&nbsp;&nbsp;&nbsp;' +
            '<span class="isRepeat"><input type="checkbox" class="checkbox" name="isrepeat" value="1" id="isrepeat_checkbox_'+num+'" '+_checkedIs(isrepeat)+'/>' +
            '<label for="isrepeat_checkbox_'+num+'">&nbsp;禁止重复</label></span>' +
            '<input type="hidden"  name="id" value="'+id+'" />' +
            '<i class="del_btn del_input"></i>' +
            '</div>' +
            '<br clear="all"/>' +
            '</div>';

        return _inputHtml;
    };

    var _selectIs = function (value,name) {
        value = value||'';name = name||'';
        if(name == value && value){
            return 'selected';
        }else{
            return '';
        }
    };

    var _checkedIs = function (value,isdef) {

         if(value == 1){

             return 'checked';
         }else{
             if(isdef){
                 return 'checked';
             }else{
                 return '';
             }
         }
    };

    var _inputArea = function (data) {
        var _html = '';
        $.each(data,function (i,e) {
            console.log(e);
            var _option = '';
            var _addOptionBtn = '';
            if(e.fieldtype == 'Radio' || e.fieldtype == 'CheckBox'){
                if(e.item){
                    $.each(e.item,function (index,el) {
                        _option += _optionHtml(el.id,el.text);
                    });
                }
                _addOptionBtn += '<div class="addItemBtnArea"><br clear="all"/><a  class="add_text">添加选项</a></div>';
            }

            _html += InputHtml(i,e.id,e.title,e.fieldtype,e.required,e.isrepeat,_option,_addOptionBtn);
        });
        return _html;
    };

    var _addInput = function (data) {
        $('body').on('click','.'+data.addInputBtn,function () {
            $('.'+data.areaClass).append(InputHtml($('.textbox').length));
        });
    };
    
    var _delInput = function (data) {
        $(document).on('click','.del_input',function () {
            var id = $(this).siblings('[name=id]').val();
            if(id){
                //有id，请求后台删除数据
                data.ajaxDelInput(id);
            }
            $(this).parents('.textbox').remove();
        });
    };
    
    var _textTypeChange = function () {
        $(document).on('change','.inputType',function () {
            var type = $(this).val();
            var t = $(this);
            var a = $(this).parents('.ipt_box2').siblings('.remarkBtn').find('.isRepeat');
            if(type == 'TextField')
            {
                _removeCaseArea(t);
                a.show();
            }
            else if (type == 'TextArea')
            {
                _removeCaseArea(t);
                a.show();
            }
            else if (type == 'Radio')
            {
                _addCaseArea(t);
                a.fadeOut();
            }
            else if (type == 'CheckBox')
            {
                _addCaseArea(t);
                a.fadeOut();
            }
            else if (type == 'UploadPic')
            {
                _removeCaseArea(t);
                a.fadeOut();
            }
            else if (type == 'UploadFile')
            {
                _removeCaseArea(t);
                a.fadeOut();
            }
        })
    };

    var _optionHtml = function (id,name) {
        id = id||'';name=name||'';
        var _opthtml = '';
        _opthtml += '<div class="opt_box">' +
            '<span class="optionText">选项</span>' +
            '<input type="text" name="case" placeholder="请输入选项名称" value="'+name+'"/>' +
            '<input type="hidden" name="caseid" value="'+id+'"/>' +
            '<i class="del_btn del_case"></i>' +
            '</div>';
        return _opthtml;
    };

    var _addCaseArea = function (t) {
        var a = t.parent('.fromGroup').siblings('.show_box').find('.caseArea');
        a.html(_optionHtml());

      if(a.next('.addItemBtnArea').length<=0){
            a.after('<div class="addItemBtnArea"><br clear="all"/><a  class="add_text">添加选项</a></div>');
        }
    };

    var _removeCaseArea = function (t) {
        t.parent('.fromGroup').siblings('.show_box').find('.caseArea').empty();
        t.parent('.fromGroup').siblings('.show_box').find('.caseArea').next('.addItemBtnArea').remove();
    };
    
    var _addOption = function () {
        $(document).on('click','.add_text',function () {
            $(this).parent('.addItemBtnArea').siblings('.caseArea').append(_optionHtml());
        });
    };
    
    var _removeOption = function (data) {
        $(document).on('click','.del_case',function () {
            var id = $(this).siblings('[name=caseid]').val();
            if(id){
                //有id，请求后台删除数据
                data.ajaxDelCase(id)
            }
            $(this).parent('.opt_box').remove();
        });
    };







})();